<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />

            <div class="q-gutter-xs" style="margin-left: 10px">
              <q-btn icon="add" no-wrap color="primary" label="创建卷子" @click="add" />
            </div>
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <!-- <q-btn flat round dense color="primary" icon="mdi-content-copy" @click="copy(props.row)">
              <q-tooltip>复制</q-tooltip>
            </q-btn> -->
            <q-btn flat round dense color="primary" icon="mdi-file-search-outline" @click="ViewExamPaper(props.row)">
              <q-tooltip>查看</q-tooltip>
            </q-btn>
            <btn-del label="试卷" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}试卷
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5>试卷名称:</h5>
              <q-input outlined dense v-model="form.paperName" type="text" />
            </div>
            <div class="col-12">
              <h5>请填写各题型题目个数:</h5>
              <div class="row">
                <div class="col-4 q-px-md">
                  <h5>选词填空:</h5>
                  <q-input outlined dense v-model="form.blankNum" type="text" />
                </div>
                <div class="col-4 q-px-md">
                  <h5>阅读理解:</h5>
                  <q-input outlined dense v-model="form.yuedukNum" type="text" />
                </div>
                <div class="col-4 q-px-md">
                  <h5>8选5:</h5>
                  <q-input outlined dense v-model="form.selectNum" type="text" />
                </div>
                <div class="col-4 q-px-md">
                  <h5>翻译:</h5>
                  <q-input outlined dense v-model="form.translateNum" type="text" />
                </div>
                <div class="col-4 q-px-md">
                  <h5>作文:</h5>
                  <q-input outlined dense v-model="form.writeNum" type="text" />
                </div>
              </div>
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <q-dialog maximized flat persistent ref="examDialog">
      <q-form @submit="submit2" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          查看试卷
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <!-- 试卷内容 -->
          <div class="q-col-gutter-x-md dialog_form q-pa-md" style="display: flex; flex-direction: column; flex-grow: 1">
            <h5>一、选词填空</h5>
            <div class="row" style="display: flex; flex-grow: 1" v-for="(item, index) in examForm.blankQuestionsList" :key="item.id">
              <div class="col-6">
                <div v-html="item.questionStem" style="border: 1px solid #ccc; padding: 10px"></div>
              </div>
              <div class="col-6">
                <!-- 出题选项卡------------------------------------------------------------------------ -->
                <div style="height: 100%">
                  <q-splitter v-model="splitterModel">
                    <template v-slot:before>
                      <q-tabs v-model="tab2[index]" vertical class="text-teal">
                        <q-tab :name="itemTab.indexNum" icon="" :label="`第${index2 + 1}题`" v-for="(itemTab, index2) in item.questionList" :key="itemTab.indexNum" />
                      </q-tabs>
                    </template>

                    <template v-slot:after>
                      <q-tab-panels v-model="tab2[index]" animated swipeable vertical transition-prev="jump-up" transition-next="jump-up">
                        <q-tab-panel :name="question.indexNum" v-for="(question, index3) in item.questionList" :key="question.indexNum">
                          <div class="row">
                            <q-radio class="col-6" v-model="examForm.blankAnswer[index][index3]" color="cyan" :label="'A: ' + question.optionA" val="A" @input="changeCheck({ value: 'A', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.blankAnswer[index][index3]" color="cyan" :label="'B: ' + question.optionB" val="B" @input="changeCheck({ value: 'B', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.blankAnswer[index][index3]" color="cyan" :label="'C: ' + question.optionC" val="C" @input="changeCheck({ value: 'C', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.blankAnswer[index][index3]" color="cyan" :label="'D: ' + question.optionD" val="D" @input="changeCheck({ value: 'D', index, index3 })" />
                            <div class="answerdiv q-pl-xl">
                              <!-- <q-input outlined dense v-model="examForm.choiceAnswer[index]" type="text" class="answer" /> -->
                              <!-- <q-icon name="mdi-check-bold" color="green" size="md" v-show="question.answer == examForm.blankAnswer[index][index3]"></q-icon> -->
                              <!-- <q-icon name="mdi-close" color="red" size="md" v-show="question.answer != examForm.blankAnswer[index][index3]"></q-icon> -->
                            </div>
                          </div>
                        </q-tab-panel>
                      </q-tab-panels>
                    </template>
                  </q-splitter>
                </div>
              </div>
            </div>
            <h5>二、阅读理解</h5>
            <div class="row" style="display: flex; flex-grow: 1" v-for="(item, index) in examForm.yueduQuestionsList" :key="item.id">
              <div class="col-6">
                <div v-html="item.questionStem" style="border: 1px solid #ccc; padding: 10px"></div>
              </div>
              <div class="col-6">
                <!-- 出题选项卡------------------------------------------------------------------------ -->
                <div style="height: 100%">
                  <q-splitter v-model="splitterModel">
                    <template v-slot:before>
                      <q-tabs v-model="tab3[index]" vertical class="text-teal">
                        <q-tab :name="itemTab.indexNum" icon="" :label="`第${index2 + 1}题`" v-for="(itemTab, index2) in item.questionList" :key="itemTab.indexNum" />
                      </q-tabs>
                    </template>

                    <template v-slot:after>
                      <q-tab-panels v-model="tab3[index]" animated swipeable vertical transition-prev="jump-up" transition-next="jump-up">
                        <q-tab-panel :name="question.indexNum" v-for="(question, index3) in item.questionList" :key="question.indexNum">
                          <div class="row">
                            <q-radio class="col-6" v-model="examForm.yueduAnswer[index][index3]" color="cyan" :label="'A: ' + question.optionA" val="A" @input="changeCheckyuedu({ value: 'A', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.yueduAnswer[index][index3]" color="cyan" :label="'B: ' + question.optionB" val="B" @input="changeCheckyuedu({ value: 'B', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.yueduAnswer[index][index3]" color="cyan" :label="'C: ' + question.optionC" val="C" @input="changeCheckyuedu({ value: 'C', index, index3 })" />
                            <q-radio class="col-6" v-model="examForm.yueduAnswer[index][index3]" color="cyan" :label="'D: ' + question.optionD" val="D" @input="changeCheckyuedu({ value: 'D', index, index3 })" />
                            <div class="answerdiv q-pl-xl">
                              <!-- <q-input outlined dense v-model="examForm.choiceAnswer[index]" type="text" class="answer" /> -->
                              <!-- <q-icon name="mdi-check-bold" color="green" size="md" v-show="question.answer == examForm.blankAnswer[index][index3]"></q-icon> -->
                              <!-- <q-icon name="mdi-close" color="red" size="md" v-show="question.answer != examForm.blankAnswer[index][index3]"></q-icon> -->
                            </div>
                          </div>
                        </q-tab-panel>
                      </q-tab-panels>
                    </template>
                  </q-splitter>
                </div>
              </div>
            </div>
            <h5>三、8选5</h5>
            <div class="row" style="display: flex; flex-grow: 1" v-for="(item, index) in examForm.selectQuestionsList" :key="item.id">
              <div class="col-6">
                <div v-html="item.questionStem" style="border: 1px solid #ccc; padding: 10px"></div>
              </div>
              <div class="col-6">
                <div style="height: 100%">
                  <!-- 出题选项卡------------------------------------------------------------------------ -->
                  <div class="">
                    <graph :orderNode="item.orderNode" :showList="false"></graph>
                  </div>
                  <div class="row" style="min-height: 200px; width: 100%; display: inline-block">
                    <div class="col-6 q-px-md">
                      <span>答案一：</span>
                      <q-input outlined dense v-model="examForm.selectAnswer[index].answer1" type="text" />
                    </div>
                    <div class="col-6 q-px-md">
                      <span>答案二：</span>
                      <q-input outlined dense v-model="examForm.selectAnswer[index].answer2" type="text" />
                    </div>
                    <div class="col-6 q-px-md">
                      <span>答案三：</span>
                      <q-input outlined dense v-model="examForm.selectAnswer[index].answer3" type="text" />
                    </div>
                    <div class="col-6 q-px-md">
                      <span>答案四：</span>
                      <q-input outlined dense v-model="examForm.selectAnswer[index].answer4" type="text" />
                    </div>
                    <div class="col-6 q-px-md">
                      <span>答案五：</span>
                      <q-input outlined dense v-model="examForm.selectAnswer[index].answer5" type="text" />
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <h5>四、翻译</h5>
            <div class="row" style="display: flex; flex-grow: 1" v-for="(item, index) in examForm.translateQuestionsList" :key="item.id">
              <div class="col-6">
                <div v-html="item.questionStem" style="border: 1px solid #ccc; padding: 10px"></div>
              </div>
              <div class="col-6">
                <!-- 出题选项卡------------------------------------------------------------------------ -->
                <div style="height: 100%">
                  <q-splitter v-model="splitterModel">
                    <template v-slot:before>
                      <q-tabs v-model="tab3[index]" vertical class="text-teal">
                        <q-tab :name="itemTab.indexNum" icon="" :label="`第${index2 + 1}题`" v-for="(itemTab, index2) in item.questionList" :key="itemTab.indexNum" />
                      </q-tabs>
                    </template>

                    <template v-slot:after>
                      <q-tab-panels v-model="tab3[index]" animated swipeable vertical transition-prev="jump-up" transition-next="jump-up">
                        <q-tab-panel :name="question.indexNum" v-for="(question, index3) in item.questionList" :key="question.indexNum">
                          <div class="row">
                            <div class="col">
                              <q-input outlined v-model="examForm.translateAnswer[index][index3]" type="textarea"></q-input>
                            </div>
                            <div class="answerdiv q-pl-xl">
                              <!-- <q-input outlined dense v-model="examForm.choiceAnswer[index]" type="text" class="answer" /> -->
                              <!-- <q-icon name="mdi-check-bold" color="green" size="md" v-show="question.answer == examForm.blankAnswer[index][index3]"></q-icon> -->
                              <!-- <q-icon name="mdi-close" color="red" size="md" v-show="question.answer != examForm.blankAnswer[index][index3]"></q-icon> -->
                            </div>
                          </div>
                        </q-tab-panel>
                      </q-tab-panels>
                    </template>
                  </q-splitter>
                </div>
              </div>
            </div>
            <h5>五、写作</h5>
            <div class="column" style="display: flex; flex-grow: 1" v-for="(item, index) in examForm.writeQuestionsList" :key="item.id">
              <div class="col-6 q-px-md">
                <div v-html="item.questionStem" style="border: 1px solid #ccc; padding: 10px"></div>
              </div>
              <div class="col-6">
                <!-- 出题选项卡------------------------------------------------------------------------ -->
                <div class="col q-px-md">
                  <span>请根据要求作答：</span>
                  <q-input outlined dense v-model="examForm.writeAnswer[index]" type="textarea" :rows="25" />
                </div>
              </div>
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit2" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import graph from '../selectFive-question/graph.vue';

export default {
  name: 'EngExamPaper',
  mixins: [IndexMixin],
  components: {
    confirm,
    graph,
  },
  data() {
    return {
      catalog: '03',
      // tab: '0101',
      tab2: [],
      tab3: [],
      tab4: [],
      splitterModel: 20,
      // index: 0,
      // index2: 0,
      form: {
        subjectId: '03',
        subjectText: '英语',
        paperName: '',
        blankNum: '',
        yuedukNum: '',
        selectNum: '',
        translateNum: '',
        writeNum: '',
      },
      emptyForm: {
        subjectId: '03',
        subjectText: '英语',
        paperName: '',
        blankNum: '',
        yuedukNum: '',
        selectNum: '',
        translateNum: '',
        writeNum: '',
      },

      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'paperName',
          align: 'center',
          label: '试卷名称',
          field: 'paperName',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      examForm: {
        id: '',
        subjectId: '03',
        subjectText: '英语',
        blankIds: '',
        yueduIds: '',
        selectIds: '',
        translateIds: '',
        writeIds: '',
        paperName: '',
        blankQuestionsList: [],
        yueduQuestionsList: [],
        selectQuestionsList: [],
        translateQuestionsList: [],
        writeQuestionsList: [],
        blankAnswer: [],
        yueduAnswer: [],
        selectAnswer: [],
        translateAnswer: [],
        writeAnswer: [],
      },
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/exam/paperEnglish/list',
        add: '/exam/paperEnglish/add',
        edit: '/exam/paperEnglish/edit',
        copy: '/exam/paperEnglish/copy',
        delete: '/exam/paperEnglish/delete',
        deleteBatch: '/exam/paperEnglish/deleteBatch',
        exportXlsUrl: '/exam/paperEnglish/exportXls',
        importExcelUrl: '/exam/paperEnglish/importExcel',
        examAdd: '/exam/paperEnglish/examAdd',
      },
    };
  },
  methods: {
    startUpload(index2, index) {
      console.log(index2, index);
      this.index = index;
      this.index2 = index2;
    },
    log() {
      // console.log(this.examForm.designAnswer);
    },
    changeCheck({ value, index, index3 }) {
      if (!this.examForm.blankAnswer[index]) {
        this.$set(this.examForm.blankAnswer, index, []);
      }
      this.$set(this.examForm.blankAnswer[index], index3, value);
      console.log(this.examForm.blankAnswer);

      this.$set(this.tab2, index, this.tab2[index]++);
      console.log(this.tab2);
    },
    changeCheckyuedu({ value, index, index3 }) {
      if (!this.examForm.yueduAnswer[index]) {
        this.$set(this.examForm.yueduAnswer, index, []);
      }
      this.$set(this.examForm.yueduAnswer[index], index3, value);
      console.log(this.examForm.yueduAnswer);

      this.$set(this.tab3, index, this.tab3[index]++);
    },
    reset() {
      this.tab = '0101';
      this.form = JSON.parse(JSON.stringify(this.emptyForm));
    },
    logTab(value, index) {
      this.tab = value;
      this.index = index;
    },
    getDictLabel,
    initDict() {},

    ViewExamPaper(row) {
      this.$axios
        .post(`${process.env.SERVER_URL}${process.env.BASE_URL}/exam/paperEnglish/getexampaper`, {
          id: row.id,
        })
        .then((result) => {
          this.examForm.paperName = result.result.paperName;
          this.examForm.id = result.result.id;
          this.examForm.createBy = result.result.createBy;
          this.examForm.blankIds = result.result.blankIds;
          this.examForm.yueduIds = result.result.yueduIds;
          this.examForm.selectIds = result.result.selectIds;
          this.examForm.translateIds = result.result.translateIds;
          this.examForm.writeIds = result.result.writeIds;
          this.examForm.blankQuestionsList = result.result.blankQuestionsList;
          this.examForm.yueduQuestionsList = result.result.yueduQuestionsList;
          this.examForm.selectQuestionsList = result.result.selectQuestionsList;
          this.examForm.translateQuestionsList = result.result.translateQuestionsList;
          this.examForm.writeQuestionsList = result.result.writeQuestionsList;
          // 初始化选词填空答案translateQuestionsList
          this.examForm.blankAnswer = [];
          this.examForm.blankQuestionsList.forEach((item, index) => {
            this.examForm.blankAnswer[index] = new Array(item.questionList.length).fill('');
          });
          this.examForm.yueduAnswer = [];
          this.examForm.yueduQuestionsList.forEach((item, index) => {
            this.examForm.yueduAnswer[index] = new Array(item.questionList.length).fill('');
          });
          this.examForm.selectAnswer = [];
          this.examForm.selectQuestionsList.forEach((item, index) => {
            this.$set(this.examForm.selectAnswer, index, {
              answer1: '',
              answer2: '',
              answer3: '',
              answer4: '',
              answer5: '',
            });
          });
          this.examForm.translateAnswer = [];
          this.examForm.translateQuestionsList.forEach((item, index) => {
            this.examForm.translateAnswer[index] = new Array(item.questionList.length).fill('');
          });
          this.examForm.writeAnswer = [];
          // this.examForm.writeQuestionsList.forEach((item, index) => {
          //   this.$set(this.examForm.writeAnswer, index, {
          //     answer: '',
          //   });
          // });
          console.log(this.examForm);
        });
      this.editType = '答题';
      this.$refs.examDialog.show();
    },
    submit2() {
      this.examForm.type = '已完成';
      this.$axios
        .post(this.url.examAdd, this.examForm)
        .then((r) => {
          this.$info(r.message);
          this.$refs.examDialog.hide();
        })
        .finally(() => {
          this.query();
        });
    },
    uploadResult(info) {
      const fileResult = JSON.parse(info.xhr.response);
      this.fileResult = fileResult;
      this.examForm.designAnswer[this.index2].questionList[this.index].viedoUrl = `http://127.0.0.1:9000${fileResult.url}`;
      console.log(this.examForm);

      this.showVideo = true;
    },
    delVideo() {
      this.$axios
        .post(`${process.env.SERVER_URL}${process.env.BASE_URL}/deleteVideo`, {
          url: this.examForm.designAnswer[this.index2].questionList[this.index].viedoUrl,
        })
        .then((r) => {
          this.$info(r.module);
          this.examForm.designAnswer[this.index2].questionList[this.index].viedoUrl = '';
        });
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus">
.answerdiv
  white-space normal
  word-wrap break-word
  overflow-wrap break-word
  display inline-block
  .answer
    display inline-flex
    max-width 500px
</style>
